import os
from datetime import datetime

from configs import config
from handlers.register import register


class Items:

    @staticmethod
    def _export_device_data(*, data):
        file_name = f'{data["sn"]}-{datetime.now().date()}.csv'
        file = f'{config.BASE_DIR}{os.sep}files{os.sep}{file_name}'
        if not os.path.exists(file):
            return False, None, None
        return True, file, file_name

    @staticmethod
    def get_devices():
        return register.get_devices()

    @staticmethod
    def _get_config_hz(*, data):
        return register.get_device_info(sn=data['sn'], attr='hz')

    @staticmethod
    def _get_config_sn(*, data):
        return register.get_device_info(sn=data['sn'], attr='sn')

    @staticmethod
    def _get_config_measurement(*, data):
        return register.get_device_info(sn=data['sn'], attr='m')

    @staticmethod
    def _get_config_decoupling(*, data):
        return register.get_device_info(sn=data['sn'], attr='de')

    @staticmethod
    def _get_device_data(*, data):
        return register.get_device_info(sn=data['sn'], attr='upload_data')

    def _set_control_collect(self, *, data):
        body = dict(sn=data['sn'], signal=data['signal'])
        return self._set(command='collect', body=body, cat='control')

    def _set_config_ip(self, *, data):
        body = dict(sn=data['sn'], origin=data['origin'], dest=data['dest'])
        return self._set(command='ip', body=body)

    def _set_config_hz(self, *, data):
        body = dict(sn=data['sn'], hz=data['hz'])
        return self._set(command='hz', body=body)

    def _set_config_sn(self, *, data):
        body = dict(sn=data['sn'], dest=data['dest'])
        return self._set(command='sn', body=body)

    def _set_config_measurement(self, *, data):
        body = dict(sn=data['sn'], m=data['mm'])
        return self._set(command='measurement', body=body)

    def _set_config_decoupling(self, *, data):
        body = dict(sn=data['sn'], de=data['de'])
        return self._set(command='decoupling', body=body)

    @staticmethod
    def _set(*, command, body, cat='config'):
        success, msg = register.start(cat=cat, command=command, data=body)
        return success, msg, None


items = Items()
